package com.hcrazy.manchester.app.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hcrazy.manchester.app.form.AttachmentForm;
import com.hcrazy.manchester.app.pojo.Attachment;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.services.AttachmentService;
import com.hcrazy.manchester.app.utils.Log;

@Controller
@RequestMapping("/admin/study")
public class AttachmentController {
	
	@Autowired
	private AttachmentService attachmentService;
	
	
	/**
	 * 跳转新增页面
	 * 初始化页面数据
	 * @param model
	 */
	@RequestMapping(value="/addAttachment",method=RequestMethod.GET)
	public void addAttachment(Model model){
		//初始化新增对象
		model.addAttribute("attachment", new Attachment());
	}
	
	/**
	 * 提交信息内容
	 * @param simple
	 * @return
	 */
	@RequestMapping(value="/attachment",method=RequestMethod.POST)
    public String addOrUpdateSort(@ModelAttribute("form") Attachment attach,Model model){
		try {
			attachmentService.addAttachment(attach);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			model.addAttribute("message", e.getMessage());
			return "/admin/failed";
		}
		return "redirect:/admin/study/attachment";
    }
	
	/**
	 * 跳转至功能信息列表页
	 * @param model
	 */
	@RequestMapping(value="/attachment",method=RequestMethod.GET)
	public void attachment(Model model){
		//根据查询参数获取分页对象：默认第一页、分类参数为0、查询字符串为空
		AttachmentForm searchForm = new AttachmentForm();
		PageModel pageModel = getAllAttachments(searchForm);
		model.addAttribute("pageModel", pageModel);
		
		//初始化查询对象
		model.addAttribute("searchForm", searchForm);
	}
	
	/**
	 * 查询信息列表
	 * @param form 查询参数form
	 * @param pageNum 当前分页
	 * @param model 请求返回对象
	 * @return
	 */
	@RequestMapping(value="/search_attach",method=RequestMethod.POST)
	public String searchAttach(@ModelAttribute("form") AttachmentForm form,Model model){
		//是否执行删除操作
		if(form.getInfoId() > 0){
			try {
				attachmentService.delAttachment(form.getInfoId());
			} catch (Exception e) {
				Log.error(e.getMessage(), e);
			}
		}
		//重置infoId
		form.setInfoId(0);
		
		//根据查询参数获取分页对象
		PageModel pageModel = getAllAttachments(form);
		model.addAttribute("pageModel", pageModel);
		
		//回传查询参数
		model.addAttribute("searchForm",form);
		
		return "/admin/study/attachment";
	}
	
	/**
	 * 查询全部信息列表
	 * @param pageNum
	 * @param form
	 * @return
	 */
	private PageModel getAllAttachments(AttachmentForm form){
		PageModel model = new PageModel();
		model.setPageNum(form.getPageNum());
		try {
			return attachmentService.getAttachments(model, form.getFileName(), form.getUsername(), form.getStartTime(), form.getEndTime(), form.getForumId());
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return null;
		}
	}

}
